/*
 * @LastEditTime: 2023-02-28 11:30:54
 */
import Axios from 'axios';
import { Singer } from "../Models/Singer.schema";
import { createSingerDTO, updateSingerDTO } from "../DTO/Singer.dto";
import { isExistsforId, isExistsforTitle } from "../hook";

export class SingerController {
  constructor(private readonly singerModel: typeof Singer) { }
  /**
   * 创建歌手
   * @param dto 数据传输对象
   * @returns 返回创建对象
   */
  async createItem(dto: createSingerDTO) {
    try {
      return {
        code: 200,
        message: "创建成功",
        data: await this.singerModel.create(dto),
      };
    } catch (error) {
      return await error;
    }
  }
  /**
 * 以ID为查找依据删除歌手
 * @param id 要删除元素的唯一ID
 * @returns
 */
  async removeById(id: string) {
    try {
      if (!await isExistsforId(id, this.singerModel)) {
        return {
          code: 400,
          message: '不存在查询的ID',
        }
      } else {
        return {
          code: 200,
          message: "删除成功",
          data: await this.singerModel.deleteOne({ _id: id }),
        };
      }
    } catch (error) {
      return await error;
    }
  }

  /**
   * 按ID更新歌手
   * @param id 要更新的ID
   * @param dto  要更新的内容
   * @returns 更新对象
   */
  async updateById(id: string, dto: updateSingerDTO) {
    try {
      if (!await isExistsforId(id, this.singerModel)) {
        return {
          code: 400,
          message: '不存在查询的ID',
        }
      } else {
        return {
          code: 200,
          message: "更新成功",
          data: await this.singerModel.updateOne({ _id: id }, dto),
        };
      }
    } catch (error) {
      return await error;
    }
  }

  /**
 * 按歌手名称查找歌手
 * @param title 要查找的歌手名称
 * @returns 查询对象
 */
  async findByTitle(title: string) {
    try {
      if (!await isExistsforTitle(title, this.singerModel)) {
        //网易云查询
        const data = await Axios.get(`http://162.14.104.16:3005/search?keywords=${title}}&type=100`, { withCredentials: true })
        const res = await data.data as {
          code: number;
          result: {
            artistCount: number;
            artists: {
              id: string;//歌手id
              name: string;  //歌手名
              picUrl: string; // 头像
              img1v1Url: string; //歌手大图
              trans: string;//歌手译名
              alias: any;//歌手标签
            }[];
          }
        };
        //  al 专辑信息
        // ar 演唱者数组
        if (res.result.artistCount > 0) {
          console.log("进来了")
          for (let i = 0; i < res.result.artists.length; i++) {
            if (i === 30) { break; }
            let tempData = {
              //歌手唯一标识
              _id: res.result.artists[i].id,
              // 歌手名称
              name: res.result.artists[i].name,
              //头像
              cover: res.result.artists[i].picUrl,
              //译名
              transName: res.result.artists[i].trans,
              //歌手大图
              img: res.result.artists[i].img1v1Url,
              //标签
              alias: res.result.artists[i].alias,
            } as createSingerDTO
            // this.createItem()

            // console.log();
            // 添加到数据库
            console.log(tempData)
            await this.createItem(tempData);
          }
          return {
            code: 200,
            message: '查询成功',
            data: await this.singerModel.find({ name: { $regex: eval("/" + title + "/g") } }),
          }
        }
        return {
          code: 400,
          message: '不存在查询的Title',
        }
      } else {
        return {
          code: 200,
          message: "查询成功",
          data: await this.singerModel.find({ name: { $regex: eval("/" + title + "/g") } }),
        };
      }
    } catch (error) {
      return await error;
    }
  }

  /**
   * 按ID查找歌手
   * @param id 要查找的歌手名称
   * @returns 查询对象
   */
  async findById(id: string) {
    try {
      if (!await isExistsforId(id, this.singerModel)) {

        return {
          code: 400,
          message: '不存在查询的ID',
        }
      } else {
        return {
          code: 200,
          message: "查询成功",
          data: await this.singerModel.find({ _id: id }),
        };
      }
    } catch (error) {
      return false;
    }
  }
  //查询全部数据
  async findAll(type: any, area: any, name: any) {
    try {
      // console.log(type, area)
      const data = await Axios.get(`http://162.14.104.16:3005/artist/list?type=${type}&limit=50&initial=${name}&area=${area}`, { withCredentials: true })
      if (data.data) {
        return {
          code: 200,
          message: "查询成功",
          data: data.data,
        }
      }
    }
    catch (error) {
      return error
    }
  }
  //获取歌手热门歌曲
  async getHot(id: string) {
    try {
      const data = await Axios.get(`http://162.14.104.16:3005/artist/top/song?id=${id}`, { withCredentials: true })
      const res = data.data as {
        code: number,
        more: boolean,
        songs: object[],
      }
      if (res) {
        return {
          code: 200,
          message: "查询成功",
          data: res.songs,
        }
      }
    }
    catch (error) {
      return error
    }

  }
  //获取歌手描述
  async getIntroduce(id: string) {
    try {
      const data = await Axios.get(`http://162.14.104.16:3005/artist/detail?id=${id}`, { withCredentials: true })
      const res = data.data as {
        code: number,
        message: string,
        data: object,
      }
      if (res) {
        return {
          code: 200,
          message: "查询成功",
          data: res.data,
        }
      }
    }
    catch (error) {
      return error
    }

  }
}